/**
 * [67] 二进制求和
 *
 * @format
 * @lc app=leetcode.cn id=67 lang=javascript
 */

// @lc code=start
/**
 * @param {string} a
 * @param {string} b
 * @return {string}
 */
var addBinary = function (a, b) {
    a = a.split("").reverse()
    b = b.split("").reverse()
    let [i, carry] = [0, 0]
    const res = []
    while (i < a.length || i < b.length) {
        res.push(a[i] ^ b[i] ^ carry)
        carry = (a[i] & b[i]) | (a[i] & carry) | (b[i] & carry)
        i++
    }
    if (carry) res.push(carry)
    res.reverse()
    return res[0] == 0 ? "0" : res.join("")
}
// 超出 INT 范围
/* var addBinary = function (a, b) {
    let sum = parseInt(a, 2)
    sum += parseInt(b, 2)
    return sum.toString(2)
} */
// @lc code=end
